package com.btpn.infra.utils;

import org.apache.commons.beanutils.BeanUtils;

public class PropertyCopier<Destination> {

	private Object sourceObject;

	private Destination destinationObject;
	
	public Destination getDestinationObject() {
		return destinationObject;
	}
	
	public PropertyCopier<Destination> setSourceObject(Object sourceObject) {
		this.sourceObject = sourceObject;
		
		return this;
	}
	
	public PropertyCopier<Destination> setDestinationObject(Destination destinationObject) {
		this.destinationObject = destinationObject;
		
		return this;
	}
	
	public PropertyCopier<Destination> copySourcePropertyToDestinationProperty(String sourcePropertyName, String destinationPropertyName) {
		try {
			Object valueToBeCopied = BeanUtils.getProperty(sourceObject, sourcePropertyName);
			BeanUtils.setProperty(destinationObject, destinationPropertyName, valueToBeCopied);
			
			return this;
		}
		catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public PropertyCopier<Destination> copyProperty(String propertyName) {
		return copySourcePropertyToDestinationProperty(propertyName, propertyName);
	}
}
